package com.Ajava1704.for设计模式.for动态代理.Cglib动态代理;

import org.springframework.cglib.proxy.Enhancer;
import org.springframework.cglib.proxy.MethodInterceptor;
import org.springframework.cglib.proxy.MethodProxy;

import java.lang.reflect.Method;

/**
 * @类名: com.Ajava1704.for设计模式.for动态代理.Cglib动态代理
 * @作者: lgt
 * @创建时间: 2020-02-14 21:47
 * @描述:
 **/
public class ForMyMethodInterceptor2<T> implements MethodInterceptor {

    private T t;

    public ForMyMethodInterceptor2(T t) {
        this.t = t;
    }

    private Enhancer enhancer = new Enhancer();

    public T getObjectProxy(Class zclass){
        enhancer.setSuperclass(zclass);
        enhancer.setCallback(this);
        return (T)enhancer.create();
    }

    @Override
    public Object intercept(Object o, Method method, Object[] objects, MethodProxy methodProxy) throws Throwable {
        System.out.println("======原方法执行前的增强=====");
        Object invoke = method.invoke(t, objects);
        System.out.println("========原方法执行后的增强=======");
        return invoke;
    }
}
